﻿// This source file is part of WallpaperControl, a tool for automatic changing 
// of the desktop wallpaper.
// 
// Visit http://www.wallpaper-control.xe.cx/ and 
// http://wpcontrol.codeplex.com/ for more.
//
// Copyright (c) 2009 Niklas Kroll
//
// Licensed under the GPLv2.
// See http://www.gnu.org/licenses/gpl-2.0.html for details.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

using System;
using System.Windows.Forms;
using System.Drawing;

namespace wpControl1
{
    class TrayApp
    {
        /// <summary>
        /// Indicates whether the TrayApp is currently active.
        /// </summary>
        internal bool Active;

        /// <summary>
        /// The icon that appears in system tray.
        /// </summary>
        internal NotifyIcon TrayIcon;

        internal ContextMenu TrayMenu = new ContextMenu();

        internal MenuItem ContextControlPanel = new MenuItem();
        internal MenuItem ContextSession = new MenuItem();
        internal MenuItem ContextPreviousWallpaper = new MenuItem();
        internal MenuItem ContextNextWallpaper = new MenuItem();
        internal MenuItem ContextExit = new MenuItem();

        /// <summary>
        /// Enters the TrayApp. The icon with the context menu is shown.
        /// </summary>
        internal void Enter()
        {
            // Create with context menu
            CreateTrayIcon();
            CreateContextMenu();
            // Show balloon tip if requested
            if (Program.SettingsManager.RunningInfo)
                TrayIcon.ShowBalloonTip(2000, "WallpaperControl", Program.LanguageManager.GetString("Tip_MinimizedDesc"), ToolTipIcon.Info);
        }

        /// <summary>
        /// Exits the TrayApp. Removes the icon from the system tray.
        /// </summary>
        internal void Exit()
        {
            // Indicate that TrayApp is inactive
            this.Active = false;
            // Hide icon
            TrayIcon.Visible = false;
        }

        /// <summary>
        /// Creates the notify icon in the system tray.
        /// </summary>
        private void CreateTrayIcon()
        {
            // Indicate that TrayApp is active
            this.Active = true;

            TrayIcon = new NotifyIcon();
            // Set icon
            TrayIcon.Icon = Properties.Resources.icon;
            // Text
            TrayIcon.Text = "WallpaperControl";
            // Open ControlPanel on doubleclick
            TrayIcon.DoubleClick += new EventHandler(TrayIcon_DoubleClick);
            // Show icon
            TrayIcon.Visible = true;
        } 

        /// <summary>
        /// Adds the context menu to the tray icon.
        /// </summary>
        private void CreateContextMenu()
        {    
            // Add items with event in correct order
            this.ContextControlPanel.Text = Program.LanguageManager.GetString("Action_ShowControlPanelText"); //System.Globalization.CultureInfo.CurrentCulture
            this.ContextControlPanel.Click += new EventHandler(itemControlPanel_Click);
            this.TrayMenu.MenuItems.Add(this.ContextControlPanel);

            this.ContextSession.Text = Program.LanguageManager.GetString("Action_ActivateText");
            this.ContextSession.Click += new EventHandler(itemSession_Click);
            this.TrayMenu.MenuItems.Add(this.ContextSession);

            this.ContextNextWallpaper.Text = Program.LanguageManager.GetString("Action_NextWallpaperText");
            this.ContextNextWallpaper.Click += new EventHandler(itemNext_Click);
            this.TrayMenu.MenuItems.Add(this.ContextNextWallpaper);

            this.ContextPreviousWallpaper.Text = Program.LanguageManager.GetString("Action_PreviousWallpaperText");
            this.ContextPreviousWallpaper.Click += new EventHandler(itemPrevious_Click);
            this.TrayMenu.MenuItems.Add(this.ContextPreviousWallpaper);

            this.ContextExit.Text = Program.LanguageManager.GetString("Action_ExitText");
            this.ContextExit.Click += new EventHandler(itemExit_Click);
            this.TrayMenu.MenuItems.Add(this.ContextExit);

            // Add this menu to icon
            TrayIcon.ContextMenu = this.TrayMenu;
        }

        #region EventHandlers
        private void TrayIcon_DoubleClick(object sender, EventArgs e)
        {
            Program.EnterControlPanel();
            Program.ControlPanel.RestoreValues();
        }

        private void itemControlPanel_Click(object sender, EventArgs e)
        {
            Program.EnterControlPanel();
            Program.ControlPanel.RestoreValues();
        }

        private void itemNext_Click(object sender, EventArgs e)
        {
            Program.WallpaperManager.SetNextWallpaper();
        }

        private void itemPrevious_Click(object sender, EventArgs e)
        {
            Program.WallpaperManager.SetPreviousWallpaper();
        }

        private void itemExit_Click(object sender, EventArgs e)
        {
            Exit();
            Application.Exit();
        }

        private void itemSession_Click(object sender, EventArgs e)
        {
            if (!Program.ChangeClient.CheckRequested)
            {
                Program.ChangeClient.StartSession();
            }
            else
            {
                Program.ChangeClient.StopSession();
            }
            Program.ControlPanel.UpdateSchedule();
        }
        #endregion
    }
}
